package com.example.JavaWebApi.core.neo4j;

import com.example.JavaWebApi.core.mybatisplusextention.wrapperCreator.ConnectorType;
import com.example.JavaWebApi.core.mybatisplusextention.wrapperCreator.OperatorType;
import com.example.JavaWebApi.core.mybatisplusextention.wrapperCreator.model.QueryConditionModel;

import javax.transaction.NotSupportedException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * @program: lvzhengcao_api
 * @description:
 * @author: 杨泽
 * @create: 2023-11-15 17:18
 */
public class Neo4jCollectionQueryWrapper extends Neo4jAbstractQueryWrapper{
    private String collParamName="collParam";
    private FuncType funcType;
    private String columnName;
    private ConnectorType connectorType;
    public Neo4jCollectionQueryWrapper(String columnName,ConnectorType connectorType,FuncType funcType){
        this.connectorType=connectorType;
        this.funcType=funcType;
        this.columnName=columnName;
    }
    public Neo4jCollectionQueryWrapper addCondition(Boolean condition, ConnectorType connectorType, OperatorType operatorType, Object value){
        if(condition){
            super.createCondition(collParamName,value,connectorType,operatorType);
        }
        return this;
    }
    public Neo4jCollectionQueryWrapper addCondition(ConnectorType connectorType, OperatorType operatorType, Object value){
        return addCondition(true,connectorType,operatorType,value);
    }

    public Neo4jCollectionQueryWrapper eq(Boolean condition,Object value){
        this.baseEq(condition,collParamName,value);
        return this;
    }
    public Neo4jCollectionQueryWrapper eq(Object value){
        return eq(true,value);
    }
    public Neo4jCollectionQueryWrapper ne(Boolean condition,Object value){
        this.baseNe(condition,collParamName,value);
        return this;
    }
    public Neo4jCollectionQueryWrapper ne(Object value){
        return ne(true,value);
    }
    public Neo4jCollectionQueryWrapper gt(Boolean condition,Object value){
        this.baseGt(condition,collParamName,value);
        return this;
    }
    public Neo4jCollectionQueryWrapper gt(Object value){
        return gt(true,value);
    }
    public Neo4jCollectionQueryWrapper ge(Boolean condition,Object value){
        this.baseGe(condition,collParamName,value);
        return this;
    }
    public Neo4jCollectionQueryWrapper ge(Object value){
        return ge(true,value);
    }
    public Neo4jCollectionQueryWrapper lt(Boolean condition,Object value){
        this.baseLt(condition,collParamName,value);
        return this;
    }
    public Neo4jCollectionQueryWrapper lt(Object value){
        return lt(true,value);
    }
    public Neo4jCollectionQueryWrapper le(Boolean condition,Object value){
        this.baseLe(condition,collParamName,value);
        return this;
    }
    public Neo4jCollectionQueryWrapper le(Object value){
        return le(true,value);
    }
    public Neo4jCollectionQueryWrapper in(Boolean condition, Collection<?> coll){
        this.baseIn(condition,collParamName,coll);
        return this;
    }
    public Neo4jCollectionQueryWrapper in(Collection<?> coll){
        return in(true,coll);
    }
    public Neo4jCollectionQueryWrapper in(Boolean condition,Object...values){
        List<?> list= Arrays.asList(values);
        return in(condition,list);
    }
    public Neo4jCollectionQueryWrapper in(Object...values){
        return in(true,values);
    }
    public Neo4jCollectionQueryWrapper notIn(Boolean condition,Collection<?> coll){
        this.baseNotIn(condition,collParamName,coll);
        return this;
    }
    public Neo4jCollectionQueryWrapper notIn(Collection<?> coll){
        return notIn(true,coll);
    }
    public Neo4jCollectionQueryWrapper like(Boolean condition,Object value){
        this.baseLike(condition,collParamName,value);
        return this;
    }
    public Neo4jCollectionQueryWrapper like(Object value){
        return like(true,value);
    }
    public Neo4jCollectionQueryWrapper notLike(Boolean condition,Object value){
        this.baseNotLike(condition,collParamName,value);
        return this;
    }
    public Neo4jCollectionQueryWrapper notLike(Object value){
        return notLike(true,value);
    }
    public Neo4jCollectionQueryWrapper likeRight(Boolean condition,Object value){
        this.baseLikeRight(condition,collParamName,value);
        return this;
    }
    public Neo4jCollectionQueryWrapper likeRight(Object value){
        return likeRight(true,value);
    }
    public Neo4jCollectionQueryWrapper likeLeft(Boolean condition,Object value){
        this.baseLikeLeft(condition,collParamName,value);
        return this;
    }
    public Neo4jCollectionQueryWrapper likeLeft(Object value){
        return likeLeft(true,value);
    }
    @Override
    public String build(Integer wrapperIndex){
        StringBuilder stringBuilder=new StringBuilder();
        for(QueryConditionModel conditionModel : conditionModelList){
            String collName=conditionModel.getColumn()+"_"+wrapperIndex+"_"+itemIndex;
            itemIndex++;
            String funcName=getFuncName();
            stringBuilder
                    .append(" ")
                    .append(getConnectSql(connectorType))
                    .append(" ")
                    .append(funcName)
                    .append("(")
                    .append(collName)
                    .append(" in ")
                    .append(columnName)
                    .append(" where 1=1 ")
                    .append(getConnectSql(conditionModel.getConnectorType())
                    +getOperatorSql(conditionModel.getOperatorType(),
                            collName,conditionModel.getValue(),wrapperIndex))
                    .append(")");
        }
        finalConditionCql=stringBuilder.toString();
        return finalConditionCql;
    }
    private String getFuncName(){
        switch (funcType){
            case NotALL:
                return " not all ";
            case Any:
                return " any ";
            case NotAny:
                return " not any ";
            case None:
                return " none";
            case NotNone:
                return " not none";
            case Single:
                return " single ";
            case NotSingle:
                return " not single ";
            default:
                return " all ";
        }
    }
}